import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "abcabcbb";
        solution.lengthOfLongestSubstring(s);
    }
}
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int left = 0;
        int right = 0;
        HashSet<Character> hash = new HashSet<>();
        char[] chars = s.toCharArray();
        int ret = 0;
        while (right<chars.length) {
            hash.add(chars[right]);
            right++;
            ret = Math.max(ret,right-left+1);
            while (right<chars.length && hash.contains(chars[right]) && chars[left]!=chars[right]) {
                hash.remove(chars[left]);
                left++;
            }
            if (right< chars.length&&chars[left] == chars[right]) {
                hash.remove(chars[left]);
                left++;
            }
        }
        return ret;
    }
}